import { CoolController, BaseController, CoolUrlTag, TagTypes } from '@cool-midway/core';
import { Inject, Post, Body } from '@midwayjs/decorator';
import { BroadcastPlayRecordEntity } from '../../entity/record';
import { BroadcastRecipeService } from '../../service/record';
import { UserInfoEntity } from '../../../user/entity/info';
import { BroadcastRecipeEntity } from '../../entity/recipe';
import { BroadcastAudioEntity } from '../../entity/audio';

/**
 * 广播-播放记录
 */
@CoolUrlTag({
  key: TagTypes.IGNORE_TOKEN,
  value: ['wxAdd', 'miniBgAudio'],
})
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: BroadcastPlayRecordEntity,
  service: BroadcastRecipeService,
  insertParam: (ctx => {
    return {
      // 获得当前登录的后台用户ID，需要请求头传Authorization参数
      userId: ctx.user.id
    }
  }),
  // 分页查询配置
  pageQueryOp: {
    keyWordLikeFields: ["title"],
    select: ['a.*', 'b.avatarUrl', 'b.nickName','c.name as audioName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BroadcastAudioEntity,
        alias: 'c',
        condition: 'a.audioId = c.id',
        type: 'leftJoin',
      }
    ],
    where: async (ctx) => {
      // 获取body参数
      return [
        ["a.userId = :userId", { userId: ctx.user.id }]
      ];
    },
  },
  listQueryOp: {
    // 让title字段支持模糊查询
    keyWordLikeFields: ['elephantNumber', 'description', 'keywords'],
    fieldEq: ['userId', 'elephantNumber', 'description', 'keywords'],
    select: ['a.*', 'b.avatarUrl', 'b.nickName','c.name as audioName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BroadcastAudioEntity,
        alias: 'c',
        condition: 'a.audioId = c.id',
        type: 'leftJoin',
      }
    ],
  },
})
export class AppBroadcastPlayRecordsController extends BaseController {
  @Inject()
  BroadcastRecipeService: BroadcastRecipeService;

  @Post('/miniBgAudio', { summary: '带背景音频的播放' })
  async miniBgAudio(@Body('value') value: string) {
    return this.ok(await this.BroadcastRecipeService.miniBgAudio(value));
  }

  @Post('/miniAudio', { summary: '不带背景音频的播放' })
  async miniAudio(@Body('value') value: string) {
    return this.ok(await this.BroadcastRecipeService.miniAudio(value));
  }

  @Post('/wxAdd')
  async wxAdd(@Body('value') value: string) {
    return this.ok(await this.BroadcastRecipeService.miniAudio(value));
  }
}
